﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MimiDouban.Abstract;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MimiDouban.UI
{
    /// <summary>
    /// 通过热键输入的UI
    /// </summary>
    public class HotKeyInputUI : Form, IInputUI
    {

        #region IInputUI Members

        public event ButtonClickHandler LikeClicked;

        public event ButtonClickHandler DisikeClicked;

        public event ButtonClickHandler NextClicked;

        public event ButtonClickHandler PauseClicked;

        public event ButtonClickHandler CloseClicked;

        public event ChannelChooseHandler ChannelChoosen;

        public event VolumeChangedHandler VolumeChanged;

        public event UserLoginHandler UserLogin;

        public void end()
        {
            UnregisterHotKey(this.Handle, 8097);
            UnregisterHotKey(this.Handle, 8098);
            UnregisterHotKey(this.Handle, 8099);
        }

        #endregion

        public HotKeyInputUI()
        {
            RegisterHotKey(this.Handle, 8097, KeyModifiers.Alt, Keys.C);
            RegisterHotKey(this.Handle, 8098, KeyModifiers.Alt, Keys.V);
            RegisterHotKey(this.Handle, 8099, KeyModifiers.Alt, Keys.B);
        }

        protected override void WndProc(ref Message m)//监视Windows消息  
        {
            const int WM_HOTKEY = 0x0312;//按快捷键  
            switch (m.Msg)
            {
                case WM_HOTKEY: 
                    ProcessHotkey(m.WParam);//调用主处理程序                      
                    break;
            } 
            base.WndProc(ref m);
        }

        public void ProcessHotkey(System.IntPtr param)
        {
            if (param.ToInt32() == 8097)
            {
                if (LikeClicked != null)
                {
                    LikeClicked.Invoke();
                }
            }
            else if (param.ToInt32() == 8098)
            {
                if (DisikeClicked != null)
                {
                    DisikeClicked.Invoke();
                }
            }
            else if (param.ToInt32() == 8099)
            {
                if (NextClicked != null)
                {
                    NextClicked.Invoke();
                }
            }
        }


        //引入两个外部函数
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(IntPtr hWnd, // handle to window   
            int id, // hot key identifier   
            KeyModifiers fsModifiers, // key-modifier options   
            Keys vk // virtual-key code   
            );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, // handle to window   
            int id // hot key identifier   
            );

        [Flags()]
        public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 }
    }
}
